
import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

import Login from '../views/login/Login'

import Home from '../views/home/Home'
import User from '../views/user/User'

import Welcome from '../views/welcome/Welcome'


import Rights from '../views/rights/Rights.vue'
import Roles from '../views/roles/Roles.vue'


import Reports from '../views/reports/Reports.vue'


import Orders from '../views/orders/Orders.vue'



const router = new VueRouter({
    routes: [
        {
            path: '/login', name: 'login', component: Login
        },


        {
            path: '/',
            name: 'Home',
            component: Home,
            redirect: {
                path: 'welcome'
            },
            children: [

                {
                    name: '',
                    path: 'welcome',
                    component: Welcome
                },

                {
                    name: 'users',
                    path: 'users',
                    component: User
                },
                {
                    name: 'rights',
                    path: 'rights',
                    component: Rights
                }
                , {
                    name: 'roles',
                    path: 'roles',
                    component: Roles
                },
                {
                    name: 'reports',
                    path: 'reports',
                    component: Reports
                },
                {
                    name: 'orders',
                    path: 'orders',
                    component: Orders
                }











            ]
        }



    ]
})





// 全局导航导航守卫
router.beforeEach((to, from, next) => {
    console.log(to);
    // 如果没有登录，但访问的是登录页面,直接进入
    if (to.path === '/login') {

        next()
    }

    else {
        let resData = JSON.parse(localStorage.getItem('userName')) || []
        // console.log(resData);
        // console.log(resData.token);

        // 如果已经登录，放行
        if (resData.token) {

            next()
        } else {
            // 如果没有登录，访问非登录页面,则跳转到登录页面

            next('/login')
        }

    }














})


export default router